#!/usr/bin/env sh
# Generate the markdown documentation table for given example routing request
# Ex:
# ./scripts/docgen_example_routes_table.sh generatetable single
# ./scripts/docgen_example_routes_table.sh generatetable multi
# ./scripts/docgen_example_routes_table.sh generatetable single
# ./scripts/docgen_example_routes_table.sh generatejsonsamples multi

ENTRIESSINGLE="
  {filter: {mdbid: [516] }, from: {lat: 40.70940, lon: -74.00537}, to: {lat: 40.72879, lon: -73.95215}, time: 2023-12-27T15:04:05Z}
  {filter: {mdbid: [1270]}, from: {lat: 50.012338, lon: 19.88192}, to: {lat: 50.08785, lon: 20.02387}, time: 2023-12-27T15:04:05Z}
  {filter: {mdbid: [1088]}, from: {lat: 50.85728, lon: 4.351426}, to: {lat: 50.83214, lon: 4.350534}, time: 2023-12-27T15:04:05Z}
  {filter: {mdbid: [1898]}, from: {lat: 47.07199, lon: 2.40154}, to: {lat: 47.10811, lon: 2.41974}, time: 2023-12-27T15:04:05Z}
  {filter: {mdbid: [1032]}, from: {lat: 38.7376, lon: -9.1725}, to: {lat: 38.72329, lon: -9.14646}, time: 2023-12-27T15:04:05Z, max_transfer_seconds: 600}
"

ENTRIESMULTI="
  {filter: {mdbid: [509, 516]}, from: {lat: 40.64809, lon: -73.93799}, to: {lat: 40.82732, lon: -74.12064}}
"

generatetabledocrow() {
  INPUT="$(echo "$1" | awk '{$1=$1}1')"
  MDBIDS_UNDERSCORED="$(echo "$INPUT" | yq '.filter.mdbid' | tr -d '[] ' | tr ',' '_')"
  MDBIDS_PLUSSED="$(echo "$INPUT" | yq '.filter.mdbid' | tr -d '[] ' | sed 's/,/ + /g')"

  FROMLAT="$(echo "$INPUT" | yq '.from.lat')"
  FROMLON="$(echo "$INPUT" | yq '.from.lon')"
  TOLAT="$(echo "$INPUT" | yq '.to.lat')"
  TOLON="$(echo "$INPUT" | yq '.to.lon')"

  # Dervived fields determined from dbstatus
  FILTER="$(echo "$INPUT" | yq '.filter')"
  STATUSJSON="$(./mobroute dbstatus -f "$FILTER")"
  COUNTRY="$(echo "$STATUSJSON" | yq 'map(.country // "Unset") | join(" + ")')"
  MUNICIPALITY="$(echo "$STATUSJSON" | yq 'map(.municipality // "Unset") | join(" + ")')"
  PROVIDERNAME="$(echo "$STATUSJSON" | yq '
    map(
      with(select(.name); .label = .provider + ": " + .name) |
      with(select(.name == null); .label = .provider) |
      .label 
    ) | join(" + ")
  ')"

  FROM="[$FROMLAT, $FROMLON](https://www.openstreetmap.org/?mlat=$FROMLAT&mlon=$FROMLON)"
  TO="[$TOLAT, $TOLON](https://www.openstreetmap.org/?mlat=$TOLAT&mlon=$TOLON)"
  echo "| $MDBIDS_PLUSSED | $COUNTRY | $MUNICIPALITY | $PROVIDERNAME | $FROM | $TO | \`$INPUT\` | [Output](doc/route_output_samples/$MDBIDS_UNDERSCORED.json) |"
}

generatetable() {
  if echo "$1" | grep -q single; then
    ENTRIES="$ENTRIESSINGLE"
  else
    ENTRIES="$ENTRIESMULTI"
  fi

  echo "| Mdbid | Country | Municipality | Name | From | To | Request YML | Output |"
  echo "| ----- | ------- | ------------ | ---- | ---- | -- | ----------- | ------ |"
  echo "$ENTRIES" | while IFS= read -r LINE; do
    echo "$LINE" | grep -Eqv '[A-Za-z0-9]+' && continue
    generatetabledocrow "$LINE" 2> /dev/null
  done
  exit 0
}

generatejsonsamples() {
  if echo "$1" | grep -q single; then
    ENTRIES="$ENTRIESSINGLE"
  else
    ENTRIES="$ENTRIESMULTI"
  fi
  
  echo "$ENTRIES" | while IFS= read -r LINE; do
    echo "$LINE" | grep -Eqv '[A-Za-z0-9]+' && continue
    REQUESTWHITESPACESTRIPPED="$(echo "$LINE" | awk '{$1=$1}1')"
    MDBIDS_UNDERSCORED="$(echo "$LINE" | yq '.filter.mdbid' | tr -d '[] ' | tr ',' '_')"
    ./mobroute route -p "$REQUESTWHITESPACESTRIPPED" > "doc/route_output_samples/$MDBIDS_UNDERSCORED".json
  done
}

"$1" "$2" || echo "Need an arg: generatejsonsamples [single|multi] or generatetable [single|multi]" >&2
